﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
using System.Windows.Threading;

namespace Monitor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        
        _2DChart VelocityChart;
        List<double> points;
        Timer Updatetimer;     
        public delegate void Update();
        System.Threading.Thread pthread;

        public MainWindow()
        {                        
            VelocityChart = new _2DChart();
            InitializeComponent();
            UpdatePausePlay(false);
            Updatetimer = new Timer();
            Updatetimer.Interval = 100;
            Updatetimer.AutoReset = true;
            Updatetimer.Enabled = false;
            Updatetimer.Elapsed += Update_Tick;
            points = new List<double>(100000);
            pthread = new System.Threading.Thread(new System.Threading.ThreadStart(GeneratorData));
            pthread.Start();
        }

        static double Step = 0;
        public void GeneratorData()
        {
            while(true)
            {
                if (Updatetimer.Enabled)
                {
                    Step += (3.14 / 30);
                    points.Add(Step);
                } 
                System.Threading.Thread.Sleep(1);
            }
        }

        static int index = 0;
        public void Update_Chart()
        {
            if(points.Count>0)
            {      
                VelocityChart.InsertNode(System.Math.Sin(points[0]));
                points.RemoveAt(0);
            }
        }

        public void Update_Tick(object sender, ElapsedEventArgs e)
        {
            VelocityChart.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, new Update(Update_Chart));
        }

        private void UpdatePausePlay(bool pause)
        {
            pausebutton.IsEnabled = pause;            
            playbutton.IsEnabled = !pause;
            intervalslider.IsEnabled = !pause;
        }


        private void multicontent_Initialized(object sender, EventArgs e)
        {
            multicontent.Content = VelocityChart;
            VelocityChart.SetChartName("Record of Velocity");
            VelocityChart.SetYAxisName("(Speed:  mm/s)");
            VelocityChart.SetXAxisName("(Time:  ms)");
        }

        private void playbutton_Click(object sender, RoutedEventArgs e)
        {
            UpdatePausePlay(true);
            Updatetimer.Enabled = true;
        }

        private void pausebutton_Click(object sender, RoutedEventArgs e)
        {
            UpdatePausePlay(false);
            Updatetimer.Enabled = false;
        }

        private void intervalslider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (Updatetimer == null)
            {
                intervalslider.Value = 100;
                return;
            }

            if(Updatetimer.Enabled == true)
            {
                return;
            }
            Updatetimer.Interval = intervalslider.Value;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            pthread.Abort();
            pthread.Join();
        }



    }

}
